package com.purong.purong.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;


import com.purong.purong.R;
import com.purong.purong.bean.AreaBean;

import java.util.List;

/**
 * Created by user on 2017/9/15.
 */
public class LocationAdapter extends HeadRecycleAdapter<AreaBean,LocationAdapter.ViewHolder> implements SectionIndexer {
    private OnAreaClickListener mListener;
    public void setOnAreaClickListener(OnAreaClickListener listener){
        this.mListener=listener;
    }
    public void updateListView(List<AreaBean> list) {
        setData(list);
        notifyDataSetChanged();
    }
    @Override
    public RecyclerView.ViewHolder onCreateView(ViewGroup parent) {
        View  v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.location_area_item, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindView(ViewHolder mHolder, final AreaBean data, final int position) {
        mHolder.areaName.setText(data.getAcronym());
        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);
        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            mHolder.letter.setVisibility(View.VISIBLE);
            mHolder.letter.setText(data.getLetters());
        } else {
            mHolder.letter.setVisibility(View.GONE);
        }
        mHolder.itemLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null){
                    mListener.onClick(data);
                }
            }
        });
    }

    @Override
    public Object[] getSections() {
        return new Object[0];
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i = 0; i < getItemCount()-1; i++) {
            String sortStr = ((AreaBean)getData().get(i)).getLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == sectionIndex) {
                return i;
            }
        }

        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        return ((AreaBean)getData().get(position)).getLetters().charAt(0);
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView letter,areaName;
        public LinearLayout itemLay;
        public ViewHolder(View v) {
            super(v);
            itemLay=(LinearLayout)v.findViewById(R.id.item_lay);
            letter = (TextView) v
                    .findViewById(R.id.catalog);
            areaName = (TextView) v
                    .findViewById(R.id.name);
        }
    }
    public interface OnAreaClickListener{
        void onClick(AreaBean area);
    }
}
